<template>
    <div>
    <div style="display: flex; justify-content: center">

        <el-input v-model="keywords" placeholder="通过用户名搜索用户" prefix-icon="el-icon-search" style="width: 400px;margin-right: 4px">

        </el-input>
        <el-button type="primary" icon="el-icon-search"  @click="doSearch">搜索</el-button>
    </div>

        <div class="admin-container">
            <el-card class="admin-card" v-for="(admin,index) in admins" :key="admin.id">
                <div slot="header" class="clearfix" >
                    <span>{{admin.name}}</span>
                    <el-button style="float: right; padding: 3px 0;color: red" type="text" icon="el-icon-delete"
                    @click="deleteAdmin(admin)">
                    </el-button>
                </div>

                <div>
                    <div class="img-container">
                        <img :src="admin.userFace" :alt="admin.name" :title="admin.name" class="userface">
                    </div>
                </div>

                <div class="userinfo-container">
                    <div>用户名:{{admin.name}}</div>
                    <div>手机号码:{{admin.phone}}</div>
                    <div>电话号码:{{admin.telephone}}</div>
                    <div>地址:{{admin.address}}</div>
                    <div style="display: flex">用户状态:{{admin.enable}}
                        <el-switch
                                @change="enableChange(admin)"
                                style="display: block"
                                v-model="admin.enabled"
                                active-color="#13ce66"
                                inactive-color="#ff4949"
                                active-text="已启用"
                                inactive-text="已禁用">
                        </el-switch>
                    </div>

                    <div>
                        用户角色：
                        <el-tag style="margin-right: 5px" type="success" v-for="(role,index) in admin.roles" :key="role.id">
                            {{role.nameZh}}
                        </el-tag>

                            <!--
                             采用elementui的Popover 弹出框
                                placement弹出位置
                            -->
                        <el-popover
                                @show="showPop(admin)"
                                @hide="hidePop(admin)"
                                placement="right"
                                title="角色列表"
                                width="200"
                                trigger="click"
                               >
                            <!--
                            multiple多选
                            根据:value值显示:label值
                            :key此属性并没有直接作用只是用来判断唯一性
                            -->
                            <el-select v-model="selectedRoles"   multiple  placeholder="请选择">
                                <el-option
                                        v-for="(r,index) in allRoles"
                                        :key="r.id"
                                        :label="r.nameZh"
                                        :value="r.id">
                                </el-option>
                            </el-select>
                            <el-button slot="reference" type="text" icon="el-icon-more"></el-button>
                        </el-popover>
                    </div>

                    <div style="color: red">
                        备注：{{admin.remark}}
                    </div>

                </div>


            </el-card>

        </div>

    </div>
</template>

<script>
    export default {
        name: "SysAdmin",
        data(){
            return{
                admins:[],
                keywords:'',
                allRoles:[],
                value:[],
                selectedRoles:[]
            }
        },
        mounted(){
          this.initadmin();
        },
        methods:{
            //关闭角色列表时触发
            hidePop(admin){
                let flag =false;
                let roles=[];
                Object.assign(roles,admin.roles);
                if (this.selectedRoles.length!=roles.length){

                    flag=true;
                }else{
                    for (let i=0;i<roles.length;i++){
                        let role =roles[i].id;
                        for (let j=0;j<this.selectedRoles.length;j++){
                            let sr=this.selectedRoles[j];
                            if (role==sr){
                                roles.splice(i,1);
                                i--;
                                break;
                            }
                        }
                    }
                    if (roles.length!=0){
                        flag=true;
                    }
                }




                if (flag){
                    let url ='/system/admin/role?adminId='+admin.id;
                    let rids ='';
                    this.selectedRoles.forEach(rid=>{
                        rids+='&rids='+rid;
                    });
                    url+=rids;

                    this.putRequest(url).then(resp=>{
                        if (resp){
                            this.initadmin();
                        }
                    });
                }

            },


            //点击角色列表时触发
            showPop(admin){
                this.initallRoles();
                let roles =admin.roles;
                this.selectedRoles=[];
                roles.forEach(r=>{
                   this.selectedRoles.push(r.id);
                });

            },
            initallRoles(){
                this.getRequest('/system/admin/roles').then(resp=>{
                   if (resp){
                       this.allRoles=resp;
                   }
                });
            },
            enableChange(admin){
                this.putRequest('/system/admin/',admin).then(resp=>{
                    if (resp){
                        this.initadmin();
                    }
                });
            },
            deleteAdmin(admin){
                this.$confirm('此操作将永久删除'+[admin.name]+', 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                   this.deleteRequest('/system/admin/'+admin.id).then(resp=>{
                      if (resp){
                          this.initadmin();
                      }
                   });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            doSearch(){
                this.initadmin();
            },
            initadmin(){
                this.getRequest('/system/admin/?keywords='+this.keywords).then(resp=>{
                    if (resp){
                        this.admins=resp;
                    }
                });
            }
        }
    }
</script>

<style scoped>
.admin-card{
    width: 350px;
    margin-bottom: 20px;
}
    .admin-container{
        display: flex;/*一行显示*/
        justify-content: space-around;/*相互之间隔开*/
        margin-top: 10px;/*顶部隔开*/
        flex-wrap:wrap;/*自动换行*/

    }
    .userface{
        width: 72px;
        height: 72px;
        border-radius: 72px;/*圆形*/
    }
    .img-container{
        width: 100%;
        display: flex;
        justify-content: center;
    }
    .userinfo-container{
        font-size: 17px;
        color: blue;
        font-family: 华文楷体;
    }
</style>
